-- UIOutBoard
-- Create by cheny3
-- 重大公告界面

-- UIOutBoard继承自Layer
UIOutBoard = class("UIOutBoard", function()
    return cc.Layer:create();
end);

function UIOutBoard.create()
    return UIOutBoard.new();
end

-- 构造函数
function UIOutBoard:ctor()
    self:setName("UIOutBoard");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/login/Login.csb");
    self:addChild(node);
    self.node = node;

    -- 检查需要弹出公告
    self:checkOutBoard();

    -- 适配
    self:resize();
end

-- 公告界面（减少自动更新前require的部分从dialog中搬出这一部分）
function UIOutBoard:showGameBoard(boardId)
    -- 根据语言版本选公告内容
    local board = GameBoardM.getGameBoardContentById(boardId);
    local data = {
        ["title"] = board["title"],
        ["subhead"] = board["showTime"],
        ["content"] = board["content"],
        ["boardId"] = boardId,
    };

    gameScene = _G["gameScene"];

    if gameScene then
        gameScene:removeFormByName("UIGameBoard");
        local uiGameBoard = UIGameBoard.create(data);
        gameScene:addForm(uiGameBoard);
    end
end

-- 检查显示公告
function UIOutBoard:checkOutBoard()
    if not _G["outBoard"] or _G["hasShowBoard"] then
        -- 没公告或者已经显示过了
        -- 开始自动更新
        clearMyRequire();
        enterAutoPatchScene();
        return false;
    end

    local bg = findChildByName(self.node, "bg");
    local ct2 = findChildByName(self.node, "ct2");
    local ct = findChildByName(self.node, "ct");
    local accountSwitch = findChildByName(self.node, "account_switch");
    local accountReg = findChildByName(self.node, "account_reg");
    local version = findChildByName(self.node, "version");

    -- 改成自动更新的底图
    local lang = UserDefaultM.getStringForKey("APP_LANG");
    if lang == nil or lang == "" then
        lang = Language.getSysLang();
    end

    if lang == "tw" then
        -- 繁体中文版本需要使用特殊的背景
        bg:loadTexture("images/ui/login/bg_tw.jpg");
    else
        bg:loadTexture("images/ui/login/bg.jpg");
    end

    -- 全部隐藏
    ct2:setVisible(false);
    ct:setVisible(false);
    accountSwitch:setVisible(false);
    accountReg:setVisible(false);
    version:setVisible(false);

    local function callback()
        -- 增加公告
        self:showGameBoard(0);
        _G["hasShowBoard"] = true;
    end
    performWithDelay(self.node, callback, 1);

    return true;
end

-- 调整位置
function UIOutBoard:resize()
    local node = self.node;

    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local bg = node:getChildByName("bg");
    bg:setPosition(w / 2, h / 2);
    local scaleCo = math.max(w / 768, h / 1136);
    bg:setScale(scaleCo);

    local mask = node:getChildByName("mask");
    mask:setPosition(w / 2, h / 2);
    mask:setContentSize(w, h);

    local ct = node:getChildByName("ct");
    ct:setPosition(w / 2, h * ct:getPositionY() / DESIGN_HEIGHT);
    ct:setScale(w / DESIGN_WIDTH);

    local bt = node:getChildByName("ct2");
    bt:setPosition(w / 2, h / 2);
    bt:setScale(math.min(w / DESIGN_WIDTH, h / (DESIGN_HEIGHT + 100)));

    local version = node:getChildByName("version");
    version:setPositionX(version:getPositionX() * w / DESIGN_WIDTH);
    version:setPositionY(version:getPositionY() * h / DESIGN_HEIGHT);
    version:setScale(w / DESIGN_WIDTH);

    local accountSwitchBtn = findChildByName(node, "account_switch");
    accountSwitchBtn:setPositionX(accountSwitchBtn:getPositionX() * w / DESIGN_WIDTH);
    accountSwitchBtn:setPositionY(accountSwitchBtn:getPositionY() * h / DESIGN_HEIGHT);
    accountSwitchBtn:setScale(accountSwitchBtn:getScale()*(w/DESIGN_WIDTH));

    local accountRegBtn = findChildByName(node, "account_reg");
    accountRegBtn:setPositionX(accountRegBtn:getPositionX() * w / DESIGN_WIDTH);
    accountRegBtn:setPositionY(accountRegBtn:getPositionY() * h / DESIGN_HEIGHT);
    accountRegBtn:setScale(accountRegBtn:getScale()*(w/DESIGN_WIDTH));

    local tipImg = findChildByName(node, "tip");
    tipImg:setPositionX(tipImg:getPositionX() * w / DESIGN_WIDTH);
    tipImg:setPositionY(tipImg:getPositionY() * h / DESIGN_HEIGHT);
    if not self.tipImgScale then
        self.tipImgScale = tipImg:getScale()*(w/DESIGN_WIDTH);
    end
    tipImg:setScale(self.tipImgScale);
end
